package com.SparkSQL

import org.apache.spark.SparkConf
import org.apache.spark.sql.SparkSession

object Spark02_SparkSQL_UDF {
  def main(args: Array[String]): Unit = {
    //创建SparkSQL运行环境
    val sparkConf = new SparkConf().setMaster("local[*]").setAppName("UDF")
    val spark: SparkSession = SparkSession.builder().config(sparkConf).getOrCreate()
    import spark.implicits._

    val df = spark.read.json("datas/user.json")
    df.createOrReplaceTempView("user")

    //自定义函数在每个username前面加一个Name
    spark.udf.register("prefixName" , (name:String)=> {
      "Name: " + name
    })

    spark.sql("select age , prefixName(username) from user").show

    //关闭连接
    spark.close()

  }

}
